package my.mark.mybaibaoxiang.logs;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;

/**
 * @author wxming.
 * @Description TODO.控制台输出，同时输出到指定文件。关键在于重写了MultiOutputStream的write方法。
 * @date 2020-8-24.
 */
public class MultiOutputStream extends OutputStream {
    OutputStream output1;
     OutputStream output2;

     public MultiOutputStream(OutputStream output1,OutputStream output2){
         this.output1 = output1;
         this.output2 = output2;
     }

     @Override
     public void write(int b) throws IOException {
         // TODO Auto-generated method stub
         output1.write(b);
         output2.write(b);
     }

    public static void main(String[] args) throws IOException {
        FileOutputStream propFile = null;
        MultiOutputStream multi = null;

        try {
            propFile = new FileOutputStream("src/applicationContext.properties");
            //设置同时输出到控制台和prop文件
            multi = new MultiOutputStream(new PrintStream(propFile),System.out);
            System.setOut(new PrintStream(multi));//TODO 此处便设置了两个输出

            System.out.println("输入任何文字均可~~");
        } catch (Exception e) {
            e.printStackTrace();
        } finally{
            multi.close();
            //input.close();
            propFile.close();
        }
    }
}


